
import { _decorator, Component, Node, Sprite, SystemEvent, EventTouch } from 'cc';
import { CommandType, Message, MessageType } from '../framework/Message';
import { MessageBase } from '../framework/MessageBase';
import { MessageCenter } from '../framework/MessageCenter';
import { GameData } from '../GameData';
import { Map } from './Map';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = MiniMap
 * DateTime = Sat Feb 05 2022 15:06:22 GMT+0800 (China Standard Time)
 * Author = vvc_zeng
 * FileBasename = MiniMap.ts
 * FileBasenameNoExtension = MiniMap
 * URL = db://assets/script/map/MiniMap.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/zh/
 *
 */

@ccclass('MiniMap')
export class MiniMap extends MessageBase {
    static Instance: MiniMap = null;

    // 小地图时的坐标位置
    areaMapPos: number;
    // 小地图数据
    miniMap: number[];

    onLoad() {
        super.onLoad()
        MiniMap.Instance = this;
    }
    init() {
        this.messageType = MessageType.TYPE_MINIMAP;
    }
    ReveiveMessage(message: Message) {
        super.ReveiveMessage(message);
        if (message.Command == CommandType.COM_MINIMAP_INIT) {
            this.initMap()
            this.node.active = true;
        }
    }
    initData() {
        this.areaMapPos = -1;
        this.miniMap = [];
    }
    initMap() {
        this.initData();
        // 显示小地图
        this.node.active = true;
        this.miniMap = Map.Instance.genMap(4);

        // 初始化第一层的地图 
        for (let i = 0; i < this.miniMap.length; i++) {
            // 创建节点，设置位置，设置图片帧，绑定点击事件
            const node = new Node(String(i));
            node.setPosition(-250 + (i % 6) * 100, 250 - (Math.floor(i / 6) * 100), 0)
            const sprite = node.addComponent(Sprite);
            const spriteFrame = this.miniMap[i][0] ? GameData.Instance.areaFrames[this.miniMap[i][1]] : GameData.Instance.monsterFrames[this.miniMap[i][1]];
            sprite.spriteFrame = spriteFrame;
            node.parent = this.node;
            if (i > 0) {
                node.active = false;
            }
            node.layer = node.parent.layer;
            node.once(SystemEvent.EventType.TOUCH_END, this.handleClick, this);
        }

        this.areaMapPos = 0;
    }

    handleClick(event: EventTouch) {
        // 其实永远只能点击到第一个节点
        let _m = <Node>(event.target)
        // 当前点击到那个下标的图块中
        let mapIndex = Number(_m.name);
        let id = this.miniMap[mapIndex];
        console.log("handleClickMiniMap", mapIndex, id);

        this.areaMapPos = mapIndex;
        // 更新视图，脚印图替换当前节点的图
        let _s = <Sprite>(_m.getComponent("cc.Sprite"))
        _s.spriteFrame = Map.Instance.goneSprite;

        if (id[0] == 0) {
            let mid = Number(id[1])
            console.log("遭遇战斗", mid);
            MessageCenter.SendCustomMessage(MessageType.TYPE_BATTLE, CommandType.COM_BATTLE_START, mid);
        } else {
            let aid = Number(id[1])
            console.log("遭遇事件", aid);
            if (aid == 3) {
                this.exit()
            } else {
                // 将事件引用传递给事件处理器
                MessageCenter.SendCustomMessage(MessageType.TYPE_EVENT, CommandType.COM_EVENT_START, aid);
            }
        }
        // 点过的节点直接销毁掉
        _m.destroy();
        // 移除第一个地图块
        this.miniMap.shift();
        // 激活下一个可点击的地图块
        if (this.node.children.length > 0) {
            this.node.children[0].active = true;
        }
    }
    exit() {
        console.log("退出小地图");
        this.areaMapPos = -1;
        this.node.active = false;
        Map.Instance.node.active = true;
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.3/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.3/manual/zh/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.3/manual/zh/scripting/life-cycle-callbacks.html
 */
